#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>

#include "statistics.h"
#include "val_est.h"
#include "shop.h"
#include "func.h"
#include "est.h"

static void scatter_diagram_points(double *ma, unsigned int n);
static void muestra_points(double *ma, unsigned int n);
static void normal_points(double *ma,unsigned int n);
static void lognormal_points(double *ma,unsigned int n);
static void gamma_points(double *ma,unsigned int n);

static double g1(double argv);
static double g2(double argv);
static double g3(double argv);

static double MU_Normal = 0.0;
static double Var_Normal = 0.0;
static double MU_LogNormal = 0.0;
static double Var_LogNormal = 0.0;
static double Alpha_Gamma = 0.0;
static double Lambda_Gamma = 0.0;

int print_estimators(double *ma, unsigned int n){
  
	MU_Normal = Estimador_MU_Normal(ma, n);
	Var_Normal = Estimador_V_Normal(ma, n);
	MU_LogNormal = Estimador_MU_Log_Normal(ma, n);
	Var_LogNormal = Estimador_V_Log_Normal(ma, n);
	Alpha_Gamma = Estimador_Alpha_Gamma(ma, n);
	Lambda_Gamma = Estimador_Lambda_Gamma(Alpha_Gamma, ma, n);

	
	printf("\n\n####################### Estimacion de parametros #############\n\n");
	printf("\tNormal\n");
	printf("\t\tMedia: %g\n", MU_Normal);
	printf("\t\tDesviacion estandar: %g\n\n", sqrt(Var_Normal));
	printf("\tLogNormal\n");
	printf("\t\tMedia: %g\n", MU_LogNormal);
	printf("\t\tDesviacion estandar: %g\n\n", sqrt(Var_LogNormal));
	printf("\tGamma\n");
	printf("\t\tAlpha: %g\n",Alpha_Gamma);
	printf("\t\tLambda: %g\n", Lambda_Gamma);
	
	return 0;
}


int print_p_valores_K_S(double *ma, unsigned int n){
	double valor_p_sim = 0.0, valor_p_u = 0.0,  d0 =0.0;
	
	printf("\n\n####################### Kolmogorov - Smirnov ################\n\n");
	printf("Hipotesis: La muestra de %i ganancias de la tienda proviene de las sgtes distros:\n",n);
	
	/* Ordeno la muestra. */
	bizzard_sort(ma, n);

	
	/************************************ Normal ****************************************/

	d0 = D(g1,ma,n);
	valor_p_sim = valor_p_KS_sim(d0,0, ma, n, MU_Normal,sqrt(Var_Normal), 1000);
	valor_p_u = valor_p_KS_u(n, d0,1000);
	
	printf("\tNormal: \n");
	printf("\t\tD: %g\n",d0);
	printf("\t\tValor-p(corregido): %g\n", valor_p_sim);
	printf("\t\tValor-p(sobrestimado) :%g\n\n", valor_p_u);
	

	
	/************************************ Log-normal ***************************************/
	
	d0 = D(g2,ma,n); 
	valor_p_sim = valor_p_KS_sim(d0,1, ma, n, MU_LogNormal,pow(Var_LogNormal,0.5), 1000);
	valor_p_u = valor_p_KS_u(n, d0,1000);
	
	printf("\tLogNormal: \n");
	printf("\t\tD: %g\n",d0);
	printf("\t\tValor-p(corregido): %g\n", valor_p_sim);
	printf("\t\tValor-p(sobrestimado) :%g\n\n", valor_p_u);

	
	/*********************************** Gamma *********************************************/
	
	d0 = D(g3,ma,n);
	valor_p_sim = valor_p_KS_sim(d0,2, ma, n, Alpha_Gamma,1.0/Lambda_Gamma, 1000);
	valor_p_u = valor_p_KS_u(n, d0,1000);
	
	printf("\tGamma: \n");
	printf("\t\tD: %g\n",d0);
	printf("\t\tValor-p(corregido): %g\n", valor_p_sim);
	printf("\t\tValor-p(Sobrestimado): %g\n\n", valor_p_u);

	return 0;

}

/*crea los archivos para luego graficar las 3 distribuciones con los parametros estimados, la muestra y el scatter diagram*/
int create_points(double *ma, unsigned int n){

	scatter_diagram_points(ma,n);
	muestra_points(ma,n);
	
	normal_points(ma,n);
	lognormal_points(ma,n);
	gamma_points(ma,n);
	
	return 0;
}


int print_p_valor_chi_square_geom(double *ma, unsigned int n){
  
	printf("\n\n####################### Chi-Cuadrado #########################\n\n");
	printf("Hipotesis: La muestra de %i pedidos en 100 dias proviene de una G(0.4)\n",n);
	
	printf("\tValor-p:%f\n",valor_p_chi_square(p_geom, ma, n, 20, 0, 20, 1));
	
	return 0;
}

int create_hists(answer shop){
	unsigned int i = 0;
	double pr=0.0;
	
	FILE *file = NULL;
	file = fopen("diagramas/puntos_geom_teo","w");
	
	for(i = 1; i<=20;i++){ 
		pr = 0.4 * pow(0.6, i-1);
		fprintf(file,"%f\n",pr);
	}
	fclose(file);
	
	file = fopen("diagramas/puntos_geom_sim","w");
	for(i=0; i < shop-> a_geom; i++)
		fprintf(file,"%f\n",shop->geom[i]);
	
	fclose(file);
	
	file = fopen("diagramas/puntos_tiempos","w");
	for(i=0; i < shop->a_arrive; i++)
		fprintf(file, "%f\n",shop->arrive[i]);
	
	fclose(file);
	
	return 0;
}


static void scatter_diagram_points(double *ma, unsigned int n){
        FILE *fd = NULL;
        unsigned int i = 0;
       
        fd = fopen("diagramas/puntos_scatter_diagram", "w");
        for(i = 0 ; i < (n - 1) ; i++)
                fprintf(fd, "%f %f\n", ma[i], ma[i+1]);
        
        fclose(fd);
}

static void muestra_points(double *ma, unsigned int n){
	FILE *fd = NULL;
	unsigned int i = 0;
	unsigned int interval=5;
	double sum = 0.0;
	
	fd = fopen("diagramas/puntos_ganancias", "w");
	bizzard_sort(ma,n);
	
	while( i < n ){
		while(i<n && ma[i] < interval){
			sum++;
			i++;
		}
		fprintf(fd, "%f %f\n", interval - 2.5, sum*0.01);
		
		if(i==n)
			break;
		sum = 0.0;
		interval+=5;
	}		
	fclose(fd);
}

/*acumulada normal con los parametros estimados*/
static double g1(double argv){
	return normalp(argv, MU_Normal, pow(Var_Normal, 0.5));
}

static void normal_points(double *ma,unsigned int n){
	unsigned int interval = 5, i=0;
	double high = 0.0, temp = 0.0;
	FILE *file=NULL;
	double sum = 0.0;
	
	file = fopen("diagramas/puntos_normal","w");
	
	for(i = 0; i < 30; i++){
	      temp = i*interval;
	      high = g1(temp + interval) - g1(temp);
	      sum+= high;
	      fprintf(file, "%f %f\n",temp + 2.5, high);
	}
	
	fclose(file);
}

/*acumulada lognormal con los parametros estimados*/
static double g2(double argv){
	return log_normalp(argv, MU_LogNormal, pow(Var_LogNormal, 0.5));
}

static void lognormal_points(double *ma,unsigned int n){
	unsigned int interval = 5, i=0;
	double high = 0.0, temp = 0.0;
	FILE *file=NULL;
	double sum = 0.0;
	
	file = fopen("diagramas/puntos_lognormal","w");
	
	for(i = 0; i < 30; i++){ /*antes de 15 la lognormal da 0*/ 
	      temp = i*interval;
	      if(i < 3)
		      high = 0.0;
	      else
		      high =  g2(temp + interval) - g2(temp);
	      sum+= high;
	      fprintf(file, "%f %f\n",temp + 2.5, high);
	}
	
	fclose(file);
}

/*acumulada gamma con los parametros estimados*/
static double g3(double argv){
	return gammap(argv, Alpha_Gamma, 1.0/Lambda_Gamma);

}

static void gamma_points(double *ma,unsigned int n){
	unsigned int interval = 5, i=0;
	double high = 0.0, temp = 0.0;
	FILE *file=NULL;
	
	double temp1 = 0.0, temp2 = 0.0;
	
	file = fopen("diagramas/puntos_gamma","w");
	
	for(i = 0; i < 30; i++){
	      temp = i*interval;
	      temp1 = g3(temp + interval);
	      temp2 = g3(temp);
	      high =  temp1 - temp2;
	      fprintf(file, "%f %f\n",temp + 2.5, high);
	}
	
	fclose(file);
}
